<?php


class tx_zlib_viewhelper_redirect extends tx_zlib_abstract_viewhelper
{

    /**
     * @var boolean
     */
    private $cache = true;

    /**
     * @var boolean
     */
    private $clearAnyway = true;

	/**
	 * @param array $overrulePIvars
	 * @param integer $altPageId
	 * @return tx_zlib_viewhelper_url
	 */
	public function redirect(array $overrulePIvars=array(), $altPageId=0)
	{
        if (0 == func_num_args()) {
            return $this;
        }
		$this->thisPagePIvars($overrulePIvars, $this->cache, $this->clearAnyway, $altPageId);
	}

	/**
	 * @param string $action
	 * @param array $overrulePIvars
	 * @param integer $altPageId
	 */
	public function toAction($action, array $overrulePIvars=array(), $altPageId=0)
	{
	    $overrulePIvars['action'] = $action;
	    $helper = $this->getHelper()->url();
		$url = $helper->thisPagePIvars($overrulePIvars, $this->cache, $this->clearAnyway, $altPageId);
		$this->redirectAndExit($url);
	}

	/**
	 * @param string $url
	 */
	public function toUrl($url)
	{
	    header('Location: ' . $url);
	    exit;
	}

	/**
	 * @param integer $id
	 * @param array $urlParameters
	 */
	public function toPage($id, array $urlParameters=array())
	{
	    $helper = $this->getHelper()->url();
		$url = $helper->toPage($id, $urlParameters);
		$this->redirectAndExit($url);
	}

	/**
	 * @param integer $id
	 * @param array $overrulePIvars
	 */
	public function toPagePIvars($id, array $overrulePIvars=array())
	{
	    $helper = $this->getHelper()->url();
	    $url = $helper->toPagePIvars($id, $overrulePIvars, $this->cache, $this->clearAnyway);
		$this->redirectAndExit($url);
	}

	/**
	 * @param array $urlParameters
	 */
	public function thisPage(array $urlParameters=array())
	{
	    $helper = $this->getHelper()->url();
		$url = $helper->thisPage($urlParameters);
		$this->redirectAndExit($url);
	}

	/**
	 * @param array $overrulePIvars
	 * @param integer $altPageId
	 */
	public function thisPagePIvars(array $overrulePIvars=array(), $altPageId=0)
	{
	    $helper = $this->getHelper()->url();
		$url = $helper->thisPagePIvars($overrulePIvars, $this->cache, $this->clearAnyway, $altPageId);
		$this->redirectAndExit($url);
	}

    /**
     * @param boolean $flag
     * @return tx_zlib_viewhelper_redirect
     */
    public function setCache($flag)
    {
        $this->cache = (bool)$flag;
        return $this;
    }

    /**
     * @param boolean $flag
     * @return tx_zlib_viewhelper_redirect
     */
    public function setClearAnyway($flag)
    {
        $this->clearAnyway = (bool)$flag;
        return $this;
    }

	/**
	 * @param string $url
	 */
	private function redirectAndExit($url)
	{
        if(strpos($url, 'http://') === false) {
            header('Location: ' . t3lib_div::getIndpEnv('TYPO3_SITE_URL') . $url);
        } else {
            header('Location: ' . $url);
        }
        exit;
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_redirect.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_redirect.php']);
}
